#+TITLE:Emacs init.el for my use on tilde.team

** bootstrap use-package
Add melpa to the package manager, install use-package. You don't have to use [[https://github.com/jwiegley/use-package][use-package]] but it does make everything nice and clean and smooth. You can also use [[https://github.com/raxod502/straight.el][straight.el]] as a package manager /(use-package is not a package manager)/ and I may do that at some point, but for right now this is working.
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (require 'package)
  ;; Any add to list for package-archives (to add marmalade or melpa) goes here
  (add-to-list 'package-archives 
	       '("MELPA" .
		 "http://melpa.milkbox.net/packages/"))
  (package-initialize)


  (unless (package-installed-p 'use-package)
    (package-refresh-contents)
    (package-install 'use-package))
  (eval-when-compile
    (require 'use-package))
  (setq use-package-always-ensure t)
#+END_SRC

** packages
Much like vim, emacs has a vibrant community of developers who have extended emacs into the swiss army knife that it is. Lots of that consists of emacs lisp packages like the three or four I am using here. 
*** avy
For me, evil-mode is just too much. I could use viper mode /(I kinda like that vim style of motion)/ but I really like emacs key bindings. To me they make sense and work for me. *But* yeah, sometimes you wanna just cut to the chase and get where you are going quick. [[https://github.com/abo-abo/avy][avy]] lets me do that. 
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  ;; avy is a GNU Emacs package for jumping to visible text using a char-based decision tree.
  ;; https://github.com/abo-abo/avy/tree/3bf83140fad4c28f2dc4c7107b9d8fef84d17cb9
  (use-package avy
    :ensure t
    :commands (avy-goto-word-0
	       avy-goto-line
	       avy-resume
	       avy-copy-line
	       avy-copy-region)
    :bind (("C-c a g w" . #'avy-goto-word-0)
	   ("C-c a g s" . #'avy-goto-subword-0)
	   ("C-c a g c" . #'avy-goto-char-timer)
	   ("C-c a g l" . #'avy-goto-line)
	   ("C-c a c l" . #'avy-copy-line)
	   ("C-c a c r" . #'avy-copy-region)
	   ("C-c a C-j" . #'avy-resume)
	   ("C-c a s l" . #'avy-kill-ring-save-whole-line)
	   ("C-c a s r" . #'avy-kill-ring-save-region)
	   ("C-c a m l" . #'avy-move-line)
	   ("C-c a m r" . #'avy-move-region)
	   ("M-g c"     . #'avy-goto-char)
	   ("M-g l"     . #'avy-goto-line)
	   ("M-g w"     . #'avy-goto-word-1)
	   ("M-z"       . #'avy-zap-to-char-dwim)
	   :map isearch-mode-map
	   ("C-'" . #'avy-isearch)))

  (use-package avy-zap
    :ensure t
    :commands (avy-zap-to-char-dwim
	       avy-zap-up-to-char-dwim)
    :bind (("C-c a z t" . #'avy-zap-to-char-dwim)
	   ("C-c a z u" . #'avy-zap-up-to-char-dwim)))
#+END_SRC

*** markdown
You need markdown-mode! Everything runs on markdown.
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (use-package auto-org-md)

  (use-package markdown-mode
    :ensure t
    :mode (("README\\.md\\'" . gfm-mode)
	   ("\\.md\\'" . markdown-mode)
	   ("\\.markdown\\'" . markdown-mode))
    :init (setq markdown-command "multimarkdown"))
#+END_SRC

*** deft
You don't exactly /need/ [[https://jblevins.org/projects/deft/][deft]], but boy is it nice. I depend on it for my personal system and couldn't imagine working without it.
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
(use-package deft
  :defer t
  :delight (deft-mode "D")
  :commands (deft)
  :config (setq deft-directory "~/documents"
		deft-extensions '("org" "md")
		deft-default-extension "org"
		deft-text-mode 'org-mode))
#+END_SRC

** Some custom stuff
*** custom.el
I don't like my init.el being cluttered up with emacs droppings so this ensures that those emacs droppings are hitting the right newspaper
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (setq custom-file "~/.emacs.d/custom.el")
  (unless (file-exists-p custom-file)
    (write-region "" nil custom-file))
  ;;; Load custom file. Don't hide errors. Hide success message
  (load custom-file nil t)
#+END_SRC

*** some ui toggles
Yeah, I don't like all the emacs defaults. Nobody does. 
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (menu-bar-mode -1) ;; don't need the menu bar especially in terminal
  (load-theme 'wombat) ;; but you want some color
  (defalias 'yes-or-no-p 'y-or-n-p) ;; change yes or no to y or n
  (show-paren-mode 1) ;; show matching parens
#+END_SRC

*** don't let emacs litter
Emacs creates backups and lockfiles. Backups are nice because they can get you out of a jam, but lockfiles aren't all that useful to me.
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (defvar --backup-directory (concat user-emacs-directory "backups"))
  (if (not (file-exists-p --backup-directory))
      (make-directory --backup-directory t))
  (setq backup-directory-alist `(("." . ,--backup-directory)))
  (setq make-backup-files t               ; backup of a file the first time it is saved.
	backup-by-copying t               ; don't clobber symlinks
	version-control t                 ; version numbers for backup files
	delete-old-versions t             ; delete excess backup files silently
	delete-by-moving-to-trash t
	create-lockfiles nil		; don't litter the system with lockfiles
	kept-old-versions 6               ; oldest versions to keep when a new numbered backup is made (default: 2)
	kept-new-versions 9               ; newest versions to keep when a new numbered backup is made (default: 2)
	auto-save-default t               ; auto-save every buffer that visits a file
	auto-save-timeout 20              ; number of seconds idle time before auto-save (default: 30)
	auto-save-interval 200            ; number of keystrokes between auto-saves (default: 300)
	)
#+END_SRC

*** ido
Ido replaces the builtin and is much nice because it gives you better feedback.
#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (setq ido-enable-flex-matching t)
  (setq ido-everywhere t)
  (setq ido-use-filename-at-point 'guess)
  (setq ido-create-new-buffer 'always)
  (setq ido-file-extensions-order '(".org" ".md" ".txt" ".py" ".emacs" ".xml" ".el" ".ini" ".cfg" ".cnf"))
  (setq linum-format "%4d ")
  (ido-mode 1)
#+END_SRC

** some little useful functions
I don't /need/ these but it is handy to have them.

#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (defun tilde/prog-mode-hook ()
    (linum-mode t))

  (defun tilde/reload-init ()
    (interactive)
    (load-file "~/.emacs.d/init.el"))

  (defun tilde/find-init ()
    (interactive)
    (find-file (concat user-emacs-directory "/init.el")))

  (add-hook 'prog-mode-hook #'tilde/prog-mode-hook)                            

#+END_SRC

** blogging
On tilde.team I use bb to post to my blog. I can 100% promise you that there is a better way to do this, but this was quick and works.

#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (defvar bb-bin "~/.local/bin/bb")
  (defvar bb-post "post " "bb post action")
  (defvar bb-edit "edit ")
  (defvar bb-delete "delete ")
  (defvar bb-rebuild "rebuild ")
  (defvar bb-reset "reset ")
  (defvar bb-list "list ")
  (defvar bb-tags "tags ")
  (defvar bb-html "-html " "Force bb to use html rather than markdown")
  (defvar bb-rename-or-list-all-tags-n "-n ")
  (defvar bb-use-full-html-f "-f ")

  (defun bash-blog (&optional action  flags filename)
    "BashBlog wrapper "
    (async-shell-command (concat bb-bin " " action flags filename) "*Bash-Blog Output*" "*Bash-Blog Error*")
    )

  (defun show-file-name ()
    "Show the full path file name in the minibuffer."
    (interactive)
    (message (buffer-file-name))
    (kill-new (file-truename buffer-file-name))
    )

  (defun post-markdown-to-bash-blog ()
    "Let bash-blog do all the work, just don't edit the file. Why isn't this an option?"
    (interactive)
    (bash-blog bb-post nil (buffer-file-name))
    (switch-to-buffer "*Bash-Blog Output*"))
#+END_SRC

** key bindings
Most of my key bindings are here. Maybe some are scattered around, but I will fix that as I go.
Some are for convenience, but some are needed because when I log in to tilde.team I use the defaul setup which is tmux. Tmux grabs *C-a* so I've replaced that binding with *M-a* not the best but it works.

#+BEGIN_SRC elisp :tangle /home/snubspreker/.emacs.d/init.el
  (global-set-key (kbd "C-x C-b") #'ibuffer) ; rebind with ibuffer
  (global-set-key (kbd "M-,") #'beginning-of-buffer)
  (global-set-key (kbd "M-.") #'end-of-buffer)
  (global-set-key (kbd "C-c f r") #'tilde/reload-init)
  (global-set-key (kbd "C-c f i") #'tilde/find-init)
  (global-set-key (kbd "M-/") #'hippie-expand)
  (global-set-key (kbd "M-a") #'beginning-of-visual-line)
  (global-set-key (kbd "M-e") #'end-of-visual-line)
  (global-set-key (kbd "C-c t r") #'ruler-mode)
  (global-set-key (kbd "C-c p m") #'post-markdown-to-bash-blog)
  (global-set-key (kbd "C-c z") 'show-file-name)
  (global-set-key (kbd "C-c d") #'deft)
#+END_SRC
